using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using Microsoft.Win32;
using System.IO;

namespace H2V2.H2Collector
{
	/// <summary>
	/// Summary description for Installer.
	/// </summary>
	[RunInstaller(true)]
	public class Installer : System.Configuration.Install.Installer
	{
		private System.Diagnostics.EventLog eLog;

		public Installer()
		{
			// This call is required by the Designer.
			InitializeComponent();
		}

		public override void Commit(IDictionary savedState)
		{
			base.Commit (savedState);

			string binpath = this.Context.Parameters["binpath"];
			string launchcfg = this.Context.Parameters["launchcfg"];

			if (launchcfg == "1")
			{
				Process.Start ("\""+binpath+"SQLH2Cfg.exe\"", "/i "+binpath);
			}
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support
		/// </summary>
		private void InitializeComponent()
		{
			this.eLog = new System.Diagnostics.EventLog();
			((System.ComponentModel.ISupportInitialize)(this.eLog)).BeginInit();
			// 
			// eLog
			// 
			this.eLog.EnableRaisingEvents = true;
			this.eLog.Log = "Application";
			this.eLog.Source = "SQLH2";
			// 
			// Installer
			// 
			this.Installers.AddRange(new System.Configuration.Install.Installer[] {
			});
			((System.ComponentModel.ISupportInitialize)(this.eLog)).EndInit();

		}
		#endregion
	}

	class RInstaller : System.Configuration.Install.Installer 
	{
		public override void Install (IDictionary mySavedState)
		{
			base.Install( mySavedState );

			Guid iGUID = Guid.NewGuid ();
			RegistryKey rk;
			rk = (Registry.LocalMachine).OpenSubKey ("SOFTWARE\\Microsoft", true); 
			rk = rk.CreateSubKey ("SQLH2");
			rk.SetValue ("iGUID", iGUID.ToString ());

		}

		public override void Uninstall( IDictionary mySavedState )
		{  
	
			try 
			{
				RegistryKey rk = (Registry.LocalMachine).OpenSubKey ("SOFTWARE\\Microsoft", true); 
				rk.DeleteSubKey ("SQLH2");
			}
			catch {}
			
			base.Uninstall( mySavedState );
		}
	}

	class DbInstaller : System.Configuration.Install.Installer
	{
		public override void Install (IDictionary mySavedState)
		{
			base.Install( mySavedState );

			string StpServer = this.Context.Parameters["StpServer"];
			string StpDatabase = this.Context.Parameters["StpDatabase"];

			StreamWriter w = new StreamWriter ("H2SetupLog.txt");

			if (StpServer == null) { w.WriteLine ("StpServer is NULL"); }
			else { w.WriteLine ("StpServer = ("+StpServer+")"); }
			
			if (StpServer == null) { w.WriteLine ("StpServer is NULL"); }
			else { w.WriteLine ("StpDatabase = ("+StpDatabase+")"); }

			w.Close ();
		}

		public override void Uninstall( IDictionary mySavedState )
		{  
			base.Uninstall( mySavedState );
		}
	}
}
